<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/23
  Time: 14:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>客户流失</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
</head>
<body>
<div class="demoTable">
    <div class="layui-form-item">
    客户名称
    <div class="layui-inline">
        <input class="layui-input" name="name" id="demoReload" autocomplete="off">
    </div>
    客户状态 <button class="layui-btn" data-type="reload">搜索</button>
</div>
</div>
<table class="layui-hide" id="loss" lay-filter="user"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<table id="roleTable" class="layui-hide" lay-filter="roleTable"></table>
<script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['layer','form','table','jquery'],function () {
        var layer = layui.layer
            , form = layui.form;
        var table = layui.table;
        var $ = layui.$;
        //表单提交监听
        form.on("submit(reload)",function (data) {
            console.log(data.field);
            //发起异步请求，新增 data.field 表单数据
            $.post("${pageContext.request.contextPath}/loss/customerLossess",function (result) {
                if (result.code==0){
                    //刷新父窗口的表格 parent 父窗口对象
                    parent.layui.table.reload("reload",{
                        where:{//查询条件
                            params:data.field//会封装到LayRequest中的params中去
                        },
                        page:{
                            curr:1
                        }
                    })
                }
            },"json");
            return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })

        //方法级渲染
        table.render({
            elem: '#roleTable'
            ,url: '${pageContext.request.contextPath}/loss/customerLossess'
            ,cols: [[
                ,{field:'id', title: 'ID', width:80, hide:true, sort: true}
                ,{field:'customer', title: '客户名称', width:200,templet:function (res) {
                        return res.customer.name;
                    }}
                ,{field:'lastOrderTime', title: '上一次下单时间', width:250}
                ,{field:'lossDate', title: '流失日期', width:250}
                ,{field:'status', title: '流失状态',width:200,templet:function (res) {
                    switch (res.status) {
                        case 0:return "将要流失";break
                        case 1:return "挽回客户";break
                        case 2:return "确认流失";break
                    }
                }}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });

        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            id: demoReload.val()
                        }
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(roleTable)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
</html>
